package cn.tedu.huntingmall.service.impl;

import cn.tedu.huntingmall.commons.exception.ServiceException;
import cn.tedu.huntingmall.mapper.ShoppingCartMapper;
import cn.tedu.huntingmall.mapper.UserMapper;
import cn.tedu.huntingmall.pojo.dto.shoppingcart.ShoppingCartAddNewDTO;
import cn.tedu.huntingmall.pojo.entity.shoppingcart.ShoppingCart;
import cn.tedu.huntingmall.pojo.vo.shoppingcart.ShoppingCartStandardVO;
import cn.tedu.huntingmall.pojo.vo.user.UserStandardVO;
import cn.tedu.huntingmall.service.IShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

import static cn.tedu.huntingmall.commons.response.ServiceCode.*;


@Service
@Slf4j
public class ShoppingCartServiceImpl implements IShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public Long insert(ShoppingCartAddNewDTO shoppingCartAddNewDTO) {
        String username = shoppingCartAddNewDTO.getUsername();
        UserStandardVO user = userMapper.selectByUsername(username);
        if (user == null) {
            log.debug("未找到对应用户名:{}的商品,请稍后重试!", username);
            throw new ServiceException(NOT_FOUND, "请先登录后再尝试购买!");
        }

        log.debug("即将插入的购物车的商品信息 {}", shoppingCartAddNewDTO);
        Long commodityId = shoppingCartAddNewDTO.getCommodityId();
        ShoppingCartStandardVO shoppingCartInDB = shoppingCartMapper.selectById(commodityId);
        if (shoppingCartInDB != null) {
            log.debug("查询到已经存在的购物车商品信息 {}", shoppingCartInDB);
            Integer countInDB = shoppingCartInDB.getCount();
            countInDB += 1;
            int rows = shoppingCartMapper.updatecountById(commodityId, countInDB);
            if (rows != 1){
                log.error("服务器忙,请稍后重试!");
                throw new ServiceException(INTERNAL_SERVER_ERROR, "服务器忙,请稍后重试!");
            }
            return commodityId;
        }

        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartAddNewDTO, shoppingCart);
        Long id = userMapper.selectByUsername(shoppingCartAddNewDTO.getUsername()).getId();
        shoppingCart.setUserId(id);
        LocalDateTime now = LocalDateTime.now();
        shoppingCart.setGmtCreate(now);
        shoppingCart.setGmtModified(now);

        log.debug("即将添加的购物车商品信息 {}", shoppingCart);
        int rows = shoppingCartMapper.insert(shoppingCart);
        if (rows != 1){
            log.error("服务器忙,请稍后重试!");
            throw new ServiceException(INTERNAL_SERVER_ERROR, "服务器忙,请稍后重试!");
        }

        return shoppingCart.getId();
    }

    @Override
    public int delete(Long id, String username) {
        UserStandardVO user = userMapper.selectByUsername(username);
        if (user == null) {
            log.debug("未找到对应用户名:{}的商品,请稍后重试!", username);
            throw new ServiceException(NOT_FOUND, "请先登录后再尝试购买!");
        }

        ShoppingCartStandardVO shoppingCartInDB = shoppingCartMapper.selectById(id);
        if (shoppingCartInDB == null) {
            log.error("未找到响应id:{} 商品信息,请稍后重试!", id);
            throw new ServiceException(NOT_FOUND, "未找到响应商品信息,请稍后重试!");
        }

        int rows = shoppingCartMapper.deleteById(id);
        if (rows != 1){
            log.error("服务器忙,请稍后重试!");
            throw new ServiceException(INTERNAL_SERVER_ERROR, "服务器忙,请稍后重试!");
        }
        return 1;// 1表示删除成功
    }

    @Override
    public List<ShoppingCartStandardVO> selectByUsername(String uesrname) {
        UserStandardVO user = userMapper.selectByUsername(uesrname);
        if (user == null) {
            log.debug("未找到对应用户名:{}的商品,请稍后重试!", uesrname);
            throw new ServiceException(NOT_FOUND, "请先登录后再尝试购买!");
        }
        return shoppingCartMapper.selectByUsername(uesrname);
    }
}
